package com.zp.framework.services.test;

import javax.ws.rs.core.MediaType;

import junit.framework.Assert;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.zp.bean.common.ResultCommonVo;
import com.zp.bean.psychological.ResultBaseServiceVo;
import com.zp.bean.psychological.ResultServiceVo;
import com.zp.domain.Service;
import com.zp.util.Constant;

/**
 * test Service services function with Junit
 * 
 * @author chenlijun08@gmail.com
 * 
 * @version 1.0
 * 
 * @date 2014/3/8
 * 
 */

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:configure/applicationContext.xml" })
public class RunServiceServices {

	private static final Log logger = LogFactory
			.getLog(RunServiceServices.class);
	private static final String URI = "http://localhost:8082/ZP/rest/";
	private WebResource service;

	@Before
	public void init() {
		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		service = client.resource(URI);
		logger.info("*********** testing to initial services client ****************");
	}

	@After
	public void destory() {
		service = null;
		logger.info("*********** testing to destory services instance ******************");
	}

	@Test
	public void testAddService() {
		try {
			Service s = new Service();
			s.setContent("cessfs");
			s.setDuration(1);
			s.setIntroduce("sfsdfsdfsd");
			s.setOrganization("4028810844c8ebd90144c95b6127005f");
			s.setTitle("cessfs");
			ClientResponse response = service.path("service/addService")
					.type(MediaType.APPLICATION_JSON_TYPE)
					.post(ClientResponse.class, s);
			ResultCommonVo result = response.getEntity(ResultCommonVo.class);
			Assert.assertEquals(Constant.OK, result.getMessage());
		} catch (UniformInterfaceException e) {
			System.out.println(e.getResponse().getStatus());
			if (e.getResponse().getStatus() == 404) {
			} else if (e.getResponse().getStatus() == 401) {
				throw new IllegalStateException(e);
			} else if (e.getResponse().getStatus() == 403) {
				throw new IllegalStateException(e);
			} else {
				throw e;
			}
		}

	}

	/**@Test**/
	public void testGetServiceById() {
		ClientResponse response = service.path("service/getService")
				.queryParam("serviceId", "402881e544a211e70144a2126c3a0001")
				.post(ClientResponse.class);
		ResultServiceVo result = response.getEntity(ResultServiceVo.class);
		Assert.assertNotNull(result.getVo());
	}

	/**@Test**/
	public void testGetServices() {
		ClientResponse response = service.path("service/getServices")
				.queryParam("pageNo", "1").queryParam("pageSize", "2")
				.post(ClientResponse.class);
		ResultBaseServiceVo result = response
				.getEntity(ResultBaseServiceVo.class);
		Assert.assertEquals(1, result.getTotalCount());
	}
}
